package com.situ.science.controller;

import com.situ.science.annotation.MyLog;
import com.situ.science.pojo.Project;
import com.situ.science.pojo.Researchers;
import com.situ.science.pojo.query.ProjectQuery;
import com.situ.science.pojo.vo.ProjectVo;
import com.situ.science.pojo.vo.ResearchersVo;
import com.situ.science.service.IProjectService;
import com.situ.science.utils.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private IProjectService projectService;
    @RequestMapping("/list")
    public PageRusults list(ProjectQuery projectQuery) {
        System.out.println("传过来的参数是:"+projectQuery);
        PageRusults pageRusults = projectService.list(projectQuery);
        return pageRusults;
    }
    @RequestMapping("/update")
    public Result update(@RequestBody ProjectVo projectVo) {
        projectService.update(projectVo);
        return Result.ok("更新成功");
    }
    @RequestMapping("/getById/{id}")
    public Result getById(@PathVariable Integer id) {
        System.out.println("ProjectController.selectById");
        ProjectVo projectVo = projectService.selectById(id);
        return Result.ok(projectVo);
    }
    @RequestMapping("/selectByRes")
    public void selectByRes() {
        System.out.println("ProjectController.selectByRes");
        Map<String,Object> map = ThreadLocalUtil.get();
        System.out.println(map.get("id"));
        Integer id = (Integer) map.get("id");
        projectService.selectByRes(id);
    }
    @MyLog(module = "添加")
    @RequestMapping("/add")
    public Result add(@RequestBody ProjectVo projectVo) {
        System.out.println("ProjectController.add");
        System.out.println("ProjectVo:"+projectVo);
        projectService.add(projectVo);
        return Result.ok("添加成功");
    }
    @PutMapping("/deleteOne/{id}")
    public Result deleteOne(@PathVariable Integer id) {
        System.out.println("ProjectController.deleteOne");
        projectService.deleteOne(id);
        return Result.ok();
    }
    @RequestMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        System.out.println("ProjectController.deleteAll");
        projectService.deleteAll(Arrays.asList(ids));
        return Result.ok();
    }
    @RequestMapping("/addByRes")
    public Result addByRes(@RequestBody ResearchersVo researchersVo,@RequestHeader(name = "Authorization") String token) {
        System.out.println("ProjectController.addByRes");
        System.out.println("researchersVo:"+researchersVo);
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer id = (Integer) map.get("id");
        projectService.addByRes(researchersVo,id);
        return Result.ok();
    }
    @RequestMapping("/updateStatus")
    public Result updateStatus(@RequestBody ProjectVo projectVo) {
        System.out.println("ProjectController.updateStatus");
        projectService.updateStatus(projectVo);
        return Result.ok();
    }
    @MyLog(module = "导出Excel")
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse resp) {
        System.out.println("ProjectController.exportExcel");
        projectService.exportExcel(resp);
    }
    @RequestMapping("/importExcel")
    public Result importExcel(MultipartFile file) {
        projectService.importExcel(file);
        return Result.ok();
    }
    @RequestMapping("/selectByContestId/{id}")
    public Result selectByContestId(@PathVariable Integer contestId) {
        System.out.println("ProjectController.selectByContestId");
        List<Project> projectList = projectService.selectByContestId(contestId);
        return Result.ok(projectList);
    }
}
